//
//  KAUser.swift
//  kano
//
//  Created by Sophia on 2024/8/20.
//

import KakaJSON

struct KAUser: Convertible, Codable {
    var softwareUpdateVersions: String = ""
    var userAccountStatus: Int = 0
    var subLoanDetails: String = ""
    var transactionChannel: Int = 0
    var wechatPayReceiptId: Int = 0
    var userIdentifier: Int = 0
    var distinctUserKey: Int = 0
    var productIdentif: Bool = false
    var frozenAccountStatus: Int = 0
    var voucherImageURL: String = ""
    var externalImageFormat: String = ""
    var installTime: String = ""
    var notificationMessage: String = ""
    var loginSourceType: String = ""
    var positionOrder: String = ""
    var authorizationco: String = ""
    var authenticationToken: String = ""
    var productTitle: String = ""
    var noTransactionUserFlag: Bool = false
    var registrationStatus: Bool = false
    var bankCardPresenceIndicator: String = ""
    var guaranteeDisplayFlag: Int = 0
    var successfulDeductionCount: Int = 0
    var userChannelIdentifier: String = ""
    var renewalStatus: String = ""
    var liquidationStatus: String = ""
    var passwordSetupToken: String = ""
    var userAccountFlag: String = ""
    
    mutating func kj_didConvertToModel(from json: [String : Any]) {
        if !positionOrder.isEmpty {
            subLoanDetails = positionOrder
        }
        
        if userIdentifier != 0 {
            wechatPayReceiptId = userIdentifier
            transactionChannel = userIdentifier
        } else if wechatPayReceiptId != 0 {
            userIdentifier = wechatPayReceiptId
            transactionChannel = wechatPayReceiptId
        }
    }
    
    func merge(with other: KAUser) -> KAUser {
        var user = KAUser(
            softwareUpdateVersions: !other.softwareUpdateVersions.isEmpty ? other.softwareUpdateVersions : self.softwareUpdateVersions,
            userAccountStatus: other.userAccountStatus != 0 ? other.userAccountStatus : self.userAccountStatus,
            subLoanDetails: !other.subLoanDetails.isEmpty ? other.subLoanDetails : self.subLoanDetails,
            transactionChannel: other.transactionChannel != 0 ? other.transactionChannel : self.transactionChannel,
            wechatPayReceiptId: other.wechatPayReceiptId != 0 ? other.wechatPayReceiptId : self.wechatPayReceiptId,
            userIdentifier: other.userIdentifier != 0 ? other.userIdentifier : self.userIdentifier,
            distinctUserKey: other.distinctUserKey != 0 ? other.distinctUserKey : self.distinctUserKey,
            productIdentif: other.productIdentif,
            frozenAccountStatus: other.frozenAccountStatus != 0 ? other.frozenAccountStatus : self.frozenAccountStatus,
            voucherImageURL: !other.voucherImageURL.isEmpty ? other.voucherImageURL : self.voucherImageURL,
            externalImageFormat: !other.externalImageFormat.isEmpty ? other.externalImageFormat : self.externalImageFormat,
            installTime: !other.installTime.isEmpty ? other.installTime : self.installTime,
            notificationMessage: !other.notificationMessage.isEmpty ? other.notificationMessage : self.notificationMessage,
            loginSourceType: !other.loginSourceType.isEmpty ? other.loginSourceType : self.loginSourceType,
            positionOrder: !other.positionOrder.isEmpty ? other.positionOrder : self.positionOrder,
            authorizationco: !other.authorizationco.isEmpty ? other.authorizationco : self.authorizationco,
            authenticationToken: !other.authenticationToken.isEmpty ? other.authenticationToken : self.authenticationToken,
            productTitle: !other.productTitle.isEmpty ? other.productTitle : self.productTitle,
            noTransactionUserFlag: other.noTransactionUserFlag,
            registrationStatus: other.registrationStatus,
            bankCardPresenceIndicator: !other.bankCardPresenceIndicator.isEmpty ? other.bankCardPresenceIndicator : self.bankCardPresenceIndicator,
            guaranteeDisplayFlag: other.guaranteeDisplayFlag != 0 ? other.guaranteeDisplayFlag : self.guaranteeDisplayFlag,
            successfulDeductionCount: other.successfulDeductionCount != 0 ? other.successfulDeductionCount : self.successfulDeductionCount,
            userChannelIdentifier: !other.userChannelIdentifier.isEmpty ? other.userChannelIdentifier : self.userChannelIdentifier,
            renewalStatus: !other.renewalStatus.isEmpty ? other.renewalStatus : self.renewalStatus,
            liquidationStatus: !other.liquidationStatus.isEmpty ? other.liquidationStatus : self.liquidationStatus,
            passwordSetupToken: !other.passwordSetupToken.isEmpty ? other.passwordSetupToken : self.passwordSetupToken,
            userAccountFlag: !other.userAccountFlag.isEmpty ? other.userAccountFlag : self.userAccountFlag
        )
        
        //login -> uid
        if user.userIdentifier != 0 {
            user.wechatPayReceiptId = user.userIdentifier
            user.transactionChannel = user.userIdentifier
        }
        //register -> userId
        else if user.wechatPayReceiptId != 0 {
            user.userIdentifier = user.wechatPayReceiptId
            user.transactionChannel = user.wechatPayReceiptId
        }
        
        return user
    }
}

